/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
$(document).on('click', '#expense_tab', function(e) {
	$('#newExpenseCurrentTab').val('expense_tab');
	$('#smallTabs_content_left').show('fast');
	$('#receiptSide').show('fast');
	$('#open_dialog_receipts3_panel').show('fast');
	$('#newExpense3_expenseForm').show('fast');
	$('#newExpense3_mileageForm').hide('fast');
	$('#newExpense3_timeForm').hide('fast');
	$('#btnAddNewRow').hide('fast');
	$('#newExpense3_multipleForm').hide('fast');
	$('#expense_tab').removeClass();
	$('#expense_tab').addClass('first current');
	$('#mileage_tab').removeClass();
	$('#time_tab').removeClass();
	$('#multiple_tab').removeClass();
	// $('#time_tab').addClass('hidden last');
});
$(document).on('click', '#mileage_tab', function(e) {
	$('#newExpenseCurrentTab').val('mileage_tab');
	$('#smallTabs_content_left').show('fast');
	$('#receiptSide').show('fast');
	$('#newExpense3_expenseForm').hide('fast');
	$('#open_dialog_receipts3_panel').show('fast');
	$('#newExpense3_mileageForm').show('fast');
	$('#newExpense3_timeForm').hide('fast');
	$('#btnAddNewRow').hide('fast');
	$('#newExpense3_multipleForm').hide('fast');
	$('#mileage_tab').removeClass();
	$('#mileage_tab').addClass('first current');
	$('#time_tab').removeClass();
	$('#expense_tab').removeClass();
	$('#multiple_tab').removeClass();
	// $('#expense_tab').addClass('hidden last');
});
$(document).on('click', '#time_tab', function(e) {
	$('#newExpenseCurrentTab').val('time_tab');
	$('#smallTabs_content_left').show('fast');
	$('#receiptSide').show('fast');
	$('#open_dialog_receipts3_panel').show('fast');
	$('#btnAddNewRow').hide('fast');
	$('#newExpense3_expenseForm').hide('fast');
	$('#newExpense3_mileageForm').hide('fast');
	$('#newExpense3_timeForm').show('fast');
	$('#newExpense3_multipleForm').hide('fast');
	$('#time_tab').removeClass();
	$('#time_tab').addClass('first current');
	$('#mileage_tab').removeClass();
	// $('#mileage_tab').addClass('hidden last');
	$('#expense_tab').removeClass();
	$('#multiple_tab').removeClass();
});
$(document).on('click', '#multiple_tab', function(e) {
	$('#newExpenseCurrentTab').val('multiple_tab');
	$('#newExpense3_expenseForm').hide('fast');
	$('#newExpense3_mileageForm').hide('fast');
	$('#newExpense3_multipleForm').show('fast');
	$('#newExpense3_timeForm').hide('fast');
	$('#smallTabs_content_left').hide('fast');
	$('#receiptSide').hide('fast');
	$('#btnAddNewRow').show('fast');
	$('#multiple_tab').removeClass();
	$('#multiple_tab').addClass('first current');
	$('#mileage_tab').removeClass();
	$('#time_tab').removeClass();
	$('#expense_tab').removeClass();

});
$(document).on('click', '#createexpense_button',function(e) {
	$('#parrent_dialog_name').val('NewExpense');

	$('#dialog_newExpense3').fadeIn(300);
	$('body').append('<div id="mask"></div>');
	$('#mask').fadeIn(300);
	return false;
});

$(document).on('click','a.dialog_x',function() {
	$('#mask, div#dialog_newExpense3').fadeOut(300,
			function() {
				$('#mask').remove();
			});
	return false;
});

$(document).on('click', '.editMultiple_button', function() {

	if ($('.expensetable_checkbox:checked').length > 0) {
		$('#dialog_editMultiple').fadeIn(300);

		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300);

		return false;
	}
});

$(document).on('click','a.dialog_x',function() {
$('#mask, div#dialog_editMultiple, div#dialog_megaEdit').fadeOut(300, function() {
			$('#mask').remove();
	});
	return false;
});

$(document).on('click','.qtip',function(e) {
	$(this).siblings().removeClass(
			'current');
	$(this).addClass('current');

	if (($(e.target).parent()
			.attr('data-tab')) == 'one') {
		$('.newExpense3_one').show('fast');
		$('.newExpense3_few').hide('fast');
		$('.newExpense3_lot').hide('fast');
	} else if (($(e.target).parent()
			.attr('data-tab')) == 'few') {
		$('.newExpense3_one').hide('fast');
		$('.newExpense3_few').show('fast');
		$('.newExpense3_lot').hide('fast');
	} else if (($(e.target).parent()
			.attr('data-tab')) == 'lot') {
		$('.newExpense3_one').hide('fast');
		$('.newExpense3_few').hide('fast');
		$('.newExpense3_lot').show('fast');
	}
});

$(document).on('click','#mergeExpense_button',function() {
	if ($('.expensetable_checkbox:checked').length == 2) {
		$('#dialog_mergeExpense').fadeIn(
				300);
		$('body').append(
				'<div id="mask"></div>');
		$('#mask').fadeIn(300);
		return false;
	} else if ($('.expensetable_checkbox:checked').length == 0) {
	} else {
		$('#dialog_1').fadeIn(300);
		$('body').append(
				'<div id="mask"></div>');
		$('#mask').fadeIn(300);
		return false;
	}
});

$(document).on(
		'click',
		'a.dialog_x',
		function() {
			$('#mask, div#dialog_mergeExpense').fadeOut(
					300, function() {
						$('#mask').remove();
					});
			return false;
		});

$(document).on('click', 'a.dialog_x', function() {
	$('#mask, div#dialog_1').fadeOut(300, function() {
		$('#mask').remove();
	});
	return false;
});

$(document).on('click','#open_dialog_receipts3,#megaEdit_attachReceipt, .addReceiptIcon',function() {
	var currentTab = $(
			'#newExpenseCurrentTab').val();
	if (currentTab == 'multiple_tab')
		$('#mutilReceiptIdCurrent').val(
				$(this).attr('id'));
	var url = getBaseUrl()+ '/expenses/loadreceipt';
	// $('#spinter-delete').show();
	var values = '';
	$.ajax( {
			url : url,
			type : "POST",
			data : values,
			beforeSend: function(){
				hideSearchLoadingSpinner();
			},
			complete: function() {
				hideSearchLoadingSpinner();
			},
			success : function(data) {
				$('#dialog_receipt')
						.html(data);
				}
			});
	$('body').append(
			'<div id="mask2"></div>');
	$('#mask2').fadeIn(300);
	$('#dialog_newExpense3').fadeOut(300);
	$('#dialog_receipt').fadeIn(300);

	return false;
});
$(document).on('click', '#open_dialog_map3', function() {
	$('#dialog_map3').fadeIn(300);
	$('body').append('<div id="mask2"></div>');
	$('#mask2').fadeIn(300);
	$('#dialog_newExpense3').fadeOut(300);

	return false;
});

$(document)
		.on(
				'click',
				'a#receipt_close',
				function() {
					$(
							'#mask2, div#dialog_receipt, div#dialog_map3')
							.fadeOut(
									300,
									function() {
										$('#mask2')
												.remove();
										$(
												'#dialog_newExpense3')
												.fadeIn(300);
										$('body')
												.append(
														'<div id="mask"></div>');
										$('#mask').fadeIn(
												300);
									});
					return false;
				});

// $('#open_dialog_creditcard').click(function() {
// $('#dialog_creditcard').fadeIn(300);
// $('body').append('<div id="mask2"></div>');
// $('#mask2').fadeIn(300);
// return false;
// });

$(document).on(
		'click',
		'a#creditcard_close',
		function() {
			$('#mask2, div#dialog_creditcard').fadeOut(
					300,
					function() {
						$('#mask2').remove();
						$('#dialog_newExpense3')
								.fadeIn(300);
						$('body').append(
								'<div id="mask"></div>');
						$('#mask').fadeIn(300);
					});
			return false;
		});
$(document)
		.on(
				'click',
				'#expenses_export_button',
				function() {
					if ($('#expenses_export_button').attr(
							'class') == 'button qtip current semwal_open') {
						$('#expenses_export_button')
								.removeClass('semwal_open');
						$('#reports_popup_export').fadeOut(
								300);
					} else {
						$('#reports_popup_export').fadeIn(
								300);
						$('#expenses_export_button')
								.addClass('semwal_open');
					}
					return false;
				});

$(document).click(function() {
	$('#reports_popup_export').fadeOut(300);
	});

});
function search() {
	var uploadURL = getBaseUrl() + '/expenses/search'; // Upload URL
	displaySearchLoadingSpinner();
	var values = $('#filter').serialize();
	$.ajax( {
		url : uploadURL,
		type : "POST",
		data : values,
		success : function(data) {
			$('#tablecontainer').html(data);
			hideSearchLoadingSpinner();
			$('html, body').animate( {
				scrollTop : 0
			}, 0);
		}
	});
}
$(document).ready(function() {
var obj = $("#qq-upload-drop-area");

obj.on('dragenter', function(e) {
e.stopPropagation();
e.preventDefault();
$(this).css('border', '2px solid #0B85A1');
});
obj.on('dragover', function(e) {
	e.stopPropagation();
	e.preventDefault();
});
obj.on('drop', function(e) {
$(this).css('border', '2px dotted #0B85A1');
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;

// We need to send dropped files to Server
	handleFileUpload(files);

});

$(document).on('dragenter', function(e) {
	e.stopPropagation();
	e.preventDefault();
});
$(document).on('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
obj.css('border', '2px dotted #0B85A1');
});
$(document).on('drop', function(e) {
	e.stopPropagation();
	e.preventDefault();
});
$('#file').change(function() {
var filedata = document.getElementById("file");
	handleFileUpload(filedata.files);
});
$(document)
		.on(
				'click',
'#btnNewExpenseSave',
function() {

	var currentTab = $(
			'#newExpenseCurrentTab').val();
	var values = '';
	var receiptId = $(
			'#newExpenseReceiptId').val();
	switch (currentTab) {
	case 'expense_tab':
		// validation
		$(
				'#newExpense3_oneExpense_merchant')
				.removeAttr("style");
		$('#newExpense3_oneExpense_amount')
				.removeAttr("style");
		var merchant = $(
				'#newExpense3_oneExpense_merchant')
				.val();
		var amount = $(
				'#newExpense3_oneExpense_amount')
				.val();
		var errorClientValidation = false;
		if (!merchant
				|| 0 === merchant.length) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneExpense_merchant')
					.css("border",
							"red solid 1px");
		}
		if (!amount || isNaN(amount)) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneExpense_amount')
					.css("border",
							"red solid 1px");
		}
		if (errorClientValidation)
			return false;
		var values = $('#expenseNewExpense')
				.serialize()
				+ '&'
				+ $('#topMenuForm')
						.serialize();
		values += '&tab=expense_tab&receiptId='
				+ receiptId;
		break;
	case 'mileage_tab':
		// validation
		$(
				'#newExpense3_oneMileage__merchant')
				.removeAttr("style");
		$(
				'#newExpense3_oneMileage_fromPosition')
				.removeAttr("style");
		$(
				'#newExpense3_oneMileage_toPosition')
				.removeAttr("style");
		$('#newExpense3_oneMileage_count')
				.removeAttr("style");
		$('#newExpense3_oneMileage_amount')
				.removeAttr("style");
		var merchant = $(
				'#newExpense3_oneMileage_merchant')
				.val();
		var from = $(
				'#newExpense3_oneMileage_fromPosition')
				.val();
		var to = $(
				'#newExpense3_oneMileage_toPosition')
				.val();
		var distance = $(
				'#newExpense3_oneMileage_count')
				.val();
		var amount = $(
				'#newExpense3_oneMileage_amount')
				.val();

		var errorClientValidation = false;
		if (!merchant
				|| 0 === merchant.length) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneMileage_merchant')
					.css("border",
							"red solid 1px");
		}
		if (!from || 0 === from.length) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneMileage_fromPosition')
					.css("border",
							"red solid 1px");
		}
		if (!to || 0 === to.length) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneMileage_toPosition')
					.css("border",
							"red solid 1px");
		}
		if (!distance || isNaN(distance)) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneMileage_count')
					.css("border",
							"red solid 1px");
		}
		if (!amount || isNaN(amount)) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneMileage_amount')
					.css("border",
							"red solid 1px");
		}
		if (errorClientValidation)
			return false;
		var values = $(
				'#newExpenseMileageForm')
				.serialize()
				+ '&'
				+ $('#topMenuForm')
						.serialize();
		values += '&tab=mileage_tab&receiptId='
				+ receiptId;
		break;
	case 'time_tab':
		// validation
		$('#newExpense3_oneTime_merchant')
				.removeAttr("style");
		$('#newExpense3_oneTime_amount')
				.removeAttr("style");
		$('#newExpense3_oneTime_hours')
				.removeAttr("style");
		$('#newExpense3_oneTime_rate')
				.removeAttr("style");
		var merchant = $(
				'#newExpense3_oneTime_merchant')
				.val();
		var amount = $(
				'#newExpense3_oneTime_amount')
				.val();
		var hour = $(
				'#newExpense3_oneTime_hours')
				.val();
		var hourRate = $(
				'#newExpense3_oneTime_rate')
				.val();
		var errorClientValidation = false;
		if (!merchant
				|| 0 === merchant.length) {
			errorClientValidation = true;
			$(
					'#newExpense3_oneTime_merchant')
					.css("border",
							"red solid 1px");
		}
		if (!amount || isNaN(amount)) {
			errorClientValidation = true;
			$('#newExpense3_oneTime_amount')
					.css("border",
							"red solid 1px");
		}
		if (!hour || isNaN(hour)) {
			errorClientValidation = true;
			$('#newExpense3_oneTime_hours')
					.css("border",
							"red solid 1px");
		}
		if (!hourRate || isNaN(hourRate)) {
			errorClientValidation = true;
			$('#newExpense3_oneTime_rate')
					.css("border",
							"red solid 1px");
		}
		if (errorClientValidation)
			return false;
		var values = $(
				'#newExpenseTimeForm')
				.serialize()
				+ '&'
				+ $('#topMenuForm')
						.serialize();
		values += '&tab=time_tab&receiptId='
				+ receiptId;
		break;
	case 'multiple_tab':
		var multiple_number_row = $(
				'#multiple_number_row')
				.val();
		var errorClientValidation = false;
		for ( var i = 0; i < multiple_number_row; i++) {
			$('#merchant_' + i).removeAttr(
					"style");
			$('#amount_' + i).removeAttr(
					"style");
			var merchant = $(
					'#merchant_' + i).val();
			var amount = $('#amount_' + i)
					.val();
			if (!merchant
					|| 0 === merchant.length) {
				errorClientValidation = true;
				$('#merchant_' + i).css(
						"border",
						"red solid 1px");
			}
			if (!amount || isNaN(amount)) {
				errorClientValidation = true;
				$('#amount_' + i).css(
						"border",
						"red solid 1px");
			}
		}
		if (errorClientValidation)
			return false;
		var values = $(
				'#newExpenseMultipleForm')
				.serialize()
				+ '&'
				+ $('#topMenuForm')
						.serialize();
		values += '&tab=multiple_tab';
		break;
	}
	$('#sipnner_newexpense').show();
	var url = getBaseUrl()
			+ '/expenses/addnewexpense';
	$.ajax( {
		url : url,
		type : "POST",
		data : values,
		success : function(data) {

		}
	});
	$('#expense_page').val('1');
	search();
	$('#sipnner_newexpense').hide();
	$('#mask, div#dialog_newExpense3')
			.fadeOut(300, function() {
				$('#mask').remove();
			});
	$("#expenseNewExpense")[0].reset();
	$("#newExpenseMileageForm")[0].reset();
	$("#newExpenseTimeForm")[0].reset();
	$("#newExpenseMultipleForm")[0].reset();
					return false;

				});

$(document)
		.on(
				'click',
'.attach_receipt_button',
function(e) {
	var idString = $(this).attr('id');
	var idArr = idString.split('_');
	var currentTab = $(
			'#newExpenseCurrentTab').val();
	var parrent_dialog = $(
			'#parrent_dialog_name').val();
	if (parrent_dialog == 'NewExpense') {
		if (currentTab == 'multiple_tab') {
			var rowId = $(
					'#mutilReceiptIdCurrent')
					.val();
			var rowIdArr = rowId.split('_');
			$(
					'#mutilReceiptId_'
							+ rowIdArr[1])
					.val(idArr[3]);
			$('#' + rowId).attr('class',
					'receiptIcon');
		} else {
			$('#newExpenseReceiptId').val(
					idArr[3]);
			$('#newExpenseReceiptImgId')
					.attr(
							'src',
							$(
									'#img_receipt_'
											+ idArr[3])
									.attr(
											'src'));

		}
	} else if (parrent_dialog == 'EditExpense') {
		$('#editReceiptId').val(idArr[3]);
		$('.receiptimage').attr(
				'src',
				$(
						'#img_receipt_'
								+ idArr[3])
						.attr('src'));
		$('#megaEdit_attachReceipt').hide(
				'fast');
		$('#megaEdit_detachExpense').show(
				'fast');
	}
	$('#mask2, div#dialog_receipt')
			.fadeOut(
					300,
					function() {
						$('#mask2')
								.remove();
						if (parrent_dialog == 'NewExpense')
							$(
									'#dialog_newExpense3')
									.fadeIn(
											300);
						else if (parrent_dialog == 'EditExpense')
							$(
									'#dialog_megaEdit')
									.fadeIn(
											300);
						$('body')
								.append(
										'<div id="mask"></div>');
						$('#mask').fadeIn(
								300);
					});
	$('#open_dialog_receipts3_panel')
			.hide();
	$('#detach').show();
					return false;
				});
$(document)
		.on(
				'click',
'#detach',
function(e) {
	$('#newExpenseReceiptImgId')
			.attr(
					'src',
					getBaseUrl()
							+ '/images/icon-receipt_150_new.png');
	$('#open_dialog_receipts3_panel')
							.show();
					$(this).hide()
				});
$(document)
		.on(
				'click',
'#expense_filter_billable_claimCustomer_All',
function(e) {
	$(this).attr('class', 'actived');
	$(
			'#expense_filter_billable_claimCustomer_Billable')
			.attr('class', '');
	$(
			'#expense_filter_billable_claimCustomer_ClaimCustomer')
			.attr('class', '');
	$('#expense_status').val('All');

				});
$(document)
		.on(
				'click',
'#expense_filter_billable_claimCustomer_Billable',
function(e) {
	$(this).attr('class', 'actived');
	$(
			'#expense_filter_billable_claimCustomer_All')
			.attr('class', '');
	$(
			'#expense_filter_billable_claimCustomer_ClaimCustomer')
			.attr('class', '');
	$('#expense_status').val('Billable');
				});
$(document)
		.on(
				'click',
'#expense_filter_billable_claimCustomer_ClaimCustomer',
function(e) {
	$(this).attr('class', 'actived');
	$(
			'#expense_filter_billable_claimCustomer_All')
			.attr('class', '');
	$(
			'#expense_filter_billable_claimCustomer_Billable')
			.attr('class', '');
	$('#expense_status').val(
			'ClaimCustomer');
				});
$(document).on(
		'click',
'.pagingink',
function(e) {
	e.preventDefault();
	var hrefString = $(this).attr('href');
var pageArray = hrefString.split('/');
$('#expense_page').val(
					pageArray[pageArray.length - 1]);
			search();

		});
$(document).on('change', '#expense_filter_status',
function(e) {
	var filterType = $(this).val();
	if (filterType != '3') {
$('#expenses_deletebutton1').show();
$('#undelete_buttons').hide();
			}
		});
$(document).on('click', '#btn_search', function(e) {
$('#filer_check_message').hide();
if ($('#startDate').val() > $('#endDate').val()) {
$('#filer_check_message').show();
	return false;
}
$('#expense_page').val('1');
var filterType = $('#expense_filter_status').val();
if (filterType == '3') {
$('#expenses_deletebutton1').hide();
$('#undelete_buttons').show();
	}
	search();
});
$(document).on('click', '#btn_reset', function(e) {
$("#filter")[0].reset();
});
$(document).on(
		'click',
'#expenses_selectall_checkbox',
function(e) {
	$('.expensetable_checkbox:input:checkbox').not(
	this).prop('checked', this.checked);
		});
$(document).on(
		'click',
'#delete_expense',
function(e) {
	var deleteURL = getBaseUrl()
			+ '/expenses/delete'; // Upload URL
// $('#spinter-delete').show();
var deleteString = '';
$('input:checkbox.expensetable_checkbox').each(
	function() {
		var sThisVal = (this.checked ? $(
				this).val() : "");
		deleteString += sThisVal + '|';
		});
$.ajax( {
	url : deleteURL,
	type : "POST",
data : 'deleteString=' + deleteString,
				success : function(data) {
				}
			});
			search();
		});
$(document).on(
		'click',
'#undelete_buttons',
function(e) {
	var deleteURL = getBaseUrl()
			+ '/expenses/undelete'; // Upload URL
// $('#spinter-delete').show();
var deleteString = '';
$('input:checkbox.expensetable_checkbox').each(
	function() {
		var sThisVal = (this.checked ? $(
				this).val() : "");
		deleteString += sThisVal + '|';
		});
$.ajax( {
	url : deleteURL,
	type : "POST",
data : 'deleteString=' + deleteString,
				success : function(data) {
				}
			});
			search();
		});
$(document)
		.on(
				'click',
'#editMultiple_save',
function(e) {
	var deleteURL = getBaseUrl()
			+ '/expenses/editmultipleexpense';
	// $('#spinter-delete').show();
	var multipleString = '';
	$(
			'input:checkbox.expensetable_checkbox')
			.each(
					function() {
						var sThisVal = (this.checked ? $(
								this).val()
								: "");
						multipleString += sThisVal
								+ '|';
					});
	$.ajax( {
		url : deleteURL,
		type : "POST",
		data : $('#editMultiple_form')
				.serialize()
				+ '&multipleString='
				+ multipleString,
		success : function(data) {
		}
	});
	search();
	$(
			'#mask, div#dialog_editMultiple, div#dialog_megaEdit')
			.fadeOut(300, function() {
				$('#mask').remove();
							});
					return false;
				});
$(document).on(
		'click',
'#addreport_button',
function(e) {

	var reportId = $('#report_selector').val();
if (reportId == 0)
	return false;
var uploadURL = getBaseUrl()
		+ '/expenses/addexpensestoreport'; // Upload
										// URL
// $('#spinter-delete').show();
var expenseString = '';
$('input:checkbox.expensetable_checkbox').each(
	function() {
		var sThisVal = (this.checked ? $(
				this).val() : "");
		expenseString += sThisVal + '|';
		});

$.ajax( {
	url : uploadURL,
	type : "POST",
data : 'expenseString=' + expenseString
		+ '&reportId=' + reportId,
				success : function(data) {
					search();
				}
			});

		});
$(document)
		.on(
				'click',
'.megaEdit,.addReceiptIcons,.receiptIcons',
function() {
	$('#parrent_dialog_name').val(
			'EditExpense');
	var url = getBaseUrl()
			+ '/expenses/editexpense';
	var idString = $(this).closest("tr")
			.attr("id");
	var idArr = idString.split('_');
	var values = 'expense_id=' + idArr[1]
			+ '&row=' + idArr[2]
			+ '&total=' + idArr[3];
	displaySearchLoadingSpinner();
	$.ajax( {
		url : url,
		type : "POST",
		data : values,
		success : function(data) {
			$('#dialog_megaEdit')
					.html(data);
			
		}
	});
	hideSearchLoadingSpinner();
	$('#dialog_megaEdit').fadeIn(300);
	$('body').append(
			'<div id="mask"></div>');
	$('#mask').fadeIn(300);
					return false;
				});
$(document).on(
		'click',
'#megaEdit_saveButton',
function() {
	var url = getBaseUrl()
			+ '/expenses/editexpense';
var values = $('#megaEdit_expenseForm')
		.serialize();
$.ajax( {
	url : url,
	type : "POST",
data : values,
success : function(data) {
	$('#mask, #dialog_megaEdit').fadeOut(
			300, function() {
				$('#mask').remove();
							});
					return false;
				}
			});

		});

$(document)
		.on(
				'click',
'#megaEdit_detachExpense',
function() {
	$('#editReceiptId').val('');
	$('.receiptimage')
			.attr(
					'src',
					getBaseUrl()
							+ '/images/icon-receipt_150_new.png');
	$('#megaEdit_attachReceipt').show(
			'fast');
	$('#megaEdit_detachExpense').hide(
			'fast');
				});

$(document).on(
		'click',
'#megaEdit_deleteButton',
function() {
	var url = getBaseUrl()
			+ '/expenses/deleteeachexpense';
var deleteExpenseId = $('#expenseId').val();
var values = 'deleteExpenseId='
		+ deleteExpenseId;
$.ajax( {
	url : url,
	type : "POST",
data : values,
success : function(data) {
	$('#mask, #dialog_megaEdit').fadeOut(
			300, function() {
				$('#mask').remove();
							});
					search();
				}
			});
		});

$(document)
		.on(
				'click',
'#megaEdit_prev',
function() {
	$('#parrent_dialog_name').val(
			'EditExpense');
	var url = getBaseUrl()
			+ '/expenses/editexpenseprevious';
	var values = 'expense_id='
			+ $('#expenseId').val()
			+ '&row='
			+ (parseInt($('#currentRow')
					.val()) - 1)
			+ '&total='
			+ $('#rowTotal').val();
	$.ajax( {
		url : url,
		type : "POST",
		data : values,
		success : function(data) {
			$('#dialog_megaEdit')
									.html(data);
						}
					});

				});
$(document)
		.on(
				'click',
'#megaEdit_next',
function() {
	$('#parrent_dialog_name').val(
			'EditExpense');
	var url = getBaseUrl()
			+ '/expenses/editexpensenext';
	var values = 'expense_id='
			+ $('#expenseId').val()
			+ '&row='
			+ (parseInt($('#currentRow')
					.val()) + 1)
			+ '&total='
			+ $('#rowTotal').val();
	$.ajax( {
		url : url,
		type : "POST",
		data : values,
		success : function(data) {
			$('#dialog_megaEdit')
									.html(data);
						}
					});
				});
$(document).on(
		'click',
'#btnAddNewRow',
function() {
	var row = $('<tr>').append(
	$('#newExpense3_expenseTable').find(
			"tbody > tr:first").clone())
		.html();
var multiple_number_row = parseInt($(
		'#multiple_number_row').val());

row = row.replace(/_0/g, "_"
		+ multiple_number_row);
$('#newExpense3_expenseTable').last().append(
		row);
multiple_number_row += 1;
$('#multiple_number_row').val(
					multiple_number_row);

		});
$(document).on(
		'keyup',
'#newExpense3_oneMileage_count',
function(e) {

	var input = parseFloat($(this).val());
	var option = $('#newExpense3_oneMileage_unit')
		.val();
var hidden_mileageRate = 0;
if (option == 'km')
hidden_mileageRate = parseFloat($(
		'#hidden_distanceRatePerKilometer')
			.val());
else if (option == 'm')
hidden_mileageRate = parseFloat($(
		'#hidden_distanceRatePerMile')
			.val());
var value = input * hidden_mileageRate;
$('#newExpense3_oneMileage_amount').val(
					value.toFixed(2));
		});
$(document).on(
		'keyup',
'#newExpense3_oneTime_hours',
function(e) {
	var input = parseFloat($(this).val());
	var oneTime_hours = parseFloat($(
			'#newExpense3_oneTime_rate').val());

var value = input * oneTime_hours;
$('#newExpense3_oneTime_amount').val(
					value.toFixed(2));
		});
$(document)
		.on(
				'change',
'#newExpense3_oneMileage_unit',
function() {
	var option = $(this).val();
	var html = '';
	var perRate = 0;
	var defaultCurrency = $(
			'#defaultCurrency').val();
	if (option == 'km') {
		perRate = parseFloat($(
				'#hidden_distanceRatePerKilometer')
				.val());
		html = 'at <a title="Change Mileage Preferences" alt="Change Mileage Preferences" href="/settings/preference">'
				+ perRate
				+ ' '
				+ defaultCurrency
				+ '</a> per km';
	} else if (option == 'm') {
		perRate = parseFloat($(
				'#hidden_distanceRatePerMile')
				.val());
		html = 'at <a title="Change Mileage Preferences" alt="Change Mileage Preferences" href="/settings/preference">'
				+ perRate
				+ ' '
				+ defaultCurrency
				+ '</a> per mile';
	}
	$('#newExpense3_mileageRate')
			.html(html);
	var input = parseFloat($(
			'#newExpense3_oneMileage_count')
			.val());
	var value = input * perRate;
	$('#newExpense3_oneMileage_amount')
							.val(value.toFixed(2));
				});
$(document).on('change', '#report_selector', function(e) {
var value = $(this).val();
if (value == 'create_new_report') {
$('#dialog_add_new_report').show();
$('#dialog_add_new_report').fadeIn(300);
$('body').append('<div id="mask2"></div>');
$('#mask2').fadeIn(300);
$(this).val('none');
		return false;
	}

});
$(document).on(
		'click',
'a.dialog_x',
function() {
	$('#mask2, div#dialog_add_new_report').fadeOut(
	300, function() {
		$('#mask2').remove();
					});
			return false;
		});
$(document)
		.on(
				'click',
'#btnNewReportSave',
function() {
	$('#dialog_add_new_report_message')
			.hide();
	$('#form_create_new_report').find(
			'input[name="report_name"]')
			.removeAttr("style");
	var reportName = $(
			'#form_create_new_report')
			.find(
					'input[name="report_name"]')
			.val();
	if (!reportName
			|| 0 === reportName.length) {
		$('#form_create_new_report')
				.find(
						'input[name="report_name"]')
				.css("border",
						"red solid 1px");
		return false;
	}
	var url = getBaseUrl()
			+ '/expenses/createnewreport';
	var values = $(
			'#form_create_new_report')
			.serialize();
	$
			.ajax( {
				url : url,
				type : "POST",
				data : values,
				dataType : "json",
				success : function(data) {
					if (data.status == 'OK') {
						$(
								'#report_selector option:first')
								.after(
										'<option value="test2" selected>'
												+ data.ReportName
												+ '</option>');
						$(
								'div#dialog_add_new_report')
								.fadeOut(
										300,
										function() {
											$(
													'#mask2')
													.remove();
										});
						$('#form_create_new_report')[0]
								.reset();
					} else {
						if (data.error_type == 'CLIENT_VALIDATION') {
							$(
									'#form_create_new_report')
									.find(
											'input[name="report_name"]')
									.css(
											"border",
											"red solid 1px");
							return false;
						} else if (data.error_type == 'SERVER_VALIDATION') {
							$(
									'#dialog_add_new_report_message')
									.html(
											'<p>'
													+ data.message
													+ '</p>');
							$(
									'#dialog_add_new_report_message')
									.show();
							return false;
						}
					}

				}
			});

	return false;
});
$(document).on('click', '#btnNewReportReset', function() {
	$('#form_create_new_report')[0].reset();
});
/**
 * For loading receipt
 */
$(document).on('click','#btn_receipt_search',function(){
		
		loadContentAttachReceiptDialog();
	});

});
